package howe.sudoku.adapter;

import howe.sudoku.blogic.GameTimeFormat;
import howe.sudoku.blogic.PuzzleBL;
import howe.sudoku.entity.SudokuInfoVo;
import howe.sudoku.layout.PuzzleView;
import howe.sudoku.layout.R;
import howe.sudoku.names.SudokuName;
import howe.sudoku.util.CheckUtils;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class SudokuListAdapter extends BaseAdapter {

    private LayoutInflater mInflater = null;
    private int iItemLayout;
    List<SudokuInfoVo> sudokuInfoList = null;
    Context mContext = null;
    private DateFormat mDateTimeFormatter = DateFormat.getDateTimeInstance(DateFormat.SHORT,
                                                                           DateFormat.SHORT);

    private DateFormat mTimeFormatter = DateFormat.getTimeInstance(DateFormat.SHORT);

    public SudokuListAdapter(Context context,
                             int iItemLayout,
                             List<SudokuInfoVo> sudokuInfoList) {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(context);
        this.iItemLayout = iItemLayout;
        this.sudokuInfoList = sudokuInfoList;
    }

    @Override
    public int getCount() {
        return sudokuInfoList.size();
    }

    @Override
    public SudokuInfoVo getItem(int position) {
        return sudokuInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(iItemLayout, null);
            holder.puzzleView = (PuzzleView)convertView.findViewById(R.id.sudoku_view);
            holder.state = (TextView)convertView.findViewById(R.id.state);
            holder.time = (TextView)convertView.findViewById(R.id.time);
            holder.lastPlayed = (TextView)convertView.findViewById(R.id.last_played);
            holder.created = (TextView)convertView.findViewById(R.id.created);
            holder.note = (TextView)convertView.findViewById(R.id.note);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }
        PuzzleBL bl = new PuzzleBL();
        bl.creatPuzzleByString(sudokuInfoList.get(position).getData());
        bl.creatReadOnlyPuzzleByString(sudokuInfoList.get(position).getReadOnlyData());
        holder.puzzleView.setPuzzleBL(bl);

        // set State
        int iState = Integer.valueOf(sudokuInfoList.get(position).getState());
        String stateString = null;
        switch (iState) {
            case SudokuName.GAME_STATE_COMPLETED:
                stateString = this.mContext.getString(R.string.list_state_solved);
                break;
            case SudokuName.GAME_STATE_PLAYING:
                stateString = this.mContext.getString(R.string.list_state_playing);
                break;
            case SudokuName.GAME_STATE_NOT_STARTED:
                stateString = this.mContext.getString(R.string.list_state_not_started);
                break;
        }
        holder.state.setVisibility(stateString == null ? View.GONE : View.VISIBLE);
        holder.state.setText(stateString);
        if (SudokuName.GAME_STATE_COMPLETED == iState) {
            holder.state.setTextColor(Color.rgb(187, 187, 187));
        } else {
            holder.state.setTextColor(Color.rgb(255, 255, 255));
        }

        // set time
        long time = -1;
        if (CheckUtils.checkRequired(sudokuInfoList.get(position).getTime())) {
            time = Long.valueOf(sudokuInfoList.get(position).getTime());
        }

        String timeString = null;
        GameTimeFormat gameTimeFormatter = new GameTimeFormat();
        if (time != 0) {
            timeString = gameTimeFormatter.format(time);
        }
        holder.time.setVisibility(timeString == null ? View.GONE : View.VISIBLE);
        holder.time.setText(timeString);
        if (SudokuName.GAME_STATE_COMPLETED == iState) {
            holder.time.setTextColor(Color.rgb(187, 187, 187));
        } else {
            holder.time.setTextColor(Color.rgb(255, 255, 255));
        }

        // set last play
        long lastPlayed = -1;
        if (CheckUtils.checkRequired(sudokuInfoList.get(position).getLastPlayed())) {
            lastPlayed = Long.valueOf(sudokuInfoList.get(position).getLastPlayed());
        }
        String lastPlayedString = null;
        if (SudokuName.GAME_STATE_NOT_STARTED != iState) {
            if (lastPlayed != 0) {
                lastPlayedString = mContext.getString(R.string.list_last_played_at,
                                                      getDateAndTimeForHumans(lastPlayed));
            }
        }
        holder.lastPlayed.setVisibility(lastPlayedString == null ? View.GONE : View.VISIBLE);
        holder.lastPlayed.setText(lastPlayedString);

        // set created
        long created = -1;
        if (CheckUtils.checkRequired(sudokuInfoList.get(position).getCreated())) {
            created = Long.valueOf(sudokuInfoList.get(position).getCreated());
        }
        String createdString = null;
        if (created != 0) {
            createdString = mContext.getString(R.string.list_created_at,
                                               getDateAndTimeForHumans(created));
        }
        holder.created.setVisibility(createdString == null ? View.INVISIBLE : View.VISIBLE);
        holder.created.setText(createdString);

        // set note
        String note = sudokuInfoList.get(position).getNote();
        if (note == null || note.trim() == "") {
            holder.note.setVisibility(View.GONE);
        } else {
            holder.note.setText(note);
            holder.note.setVisibility(View.VISIBLE);
        }
        holder.note.setText(note);
        return convertView;
    }

    static class ViewHolder {
        public PuzzleView puzzleView;
        public TextView state;
        public TextView time;
        public TextView lastPlayed;
        public TextView created;
        public TextView note;
    }

    private String getDateAndTimeForHumans(long datetime) {
        Date date = new Date(datetime);

        Date now = new Date(System.currentTimeMillis());
        Date today = new Date(now.getYear(), now.getMonth(), now.getDate());
        Date yesterday = new Date(System.currentTimeMillis()
                - (1000 * 60 * 60 * 24));
        if (date.after(today)) {
            return mContext.getString(R.string.list_at_time, mTimeFormatter.format(date));
        } else if (date.after(yesterday)) {
            return mContext.getString(R.string.list_yesterday_at_time, mTimeFormatter.format(date));
        } else {
            return mContext.getString(R.string.list_on_date, mDateTimeFormatter.format(date));
        }

    }
}